﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Epam.PhotoGallery.Interfaces.DataTransferObjects;
using Epam.PhotoGallery.Interfaces.Providers;
using System.Configuration;
using Amazon.PAAPI.WCF;
using Epam.PhotoGallery.Site.AmazonAPI;


namespace Epam.PhotoGallery.Site.Models
{
    public class OfficeLibrary
    {

        #region Constants
        private const string APP_CONFIG_AMAZON_ACCESS_KEY_SECTION = "accessKeyId";
        private const string MEMORY_MODEL_NAME = "OfficeLibrary";
        private const string BEGIN_HEADER_TAG_FRAGMENT = "<h";
        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets office library books.
        /// </summary>
        /// <value>
        /// The books.
        /// </value>
        public IEnumerable<Book> LibraryBooks
        { get; set; }

        #endregion


        /// <summary>
        /// Gets the model.
        /// </summary>
        /// <param name="provider">The provider.</param>
        /// <returns>
        /// Return OfficeLibrary.
        /// </returns>
        public static OfficeLibrary GetModel(ILibraryDataProvider provider)
        {

            OfficeLibrary result = new OfficeLibrary() { LibraryBooks = provider.GetAllBooksFromLibrary() };

            return result;
        }

        /// <summary>
        /// Gets the book details
        /// </summary>
        /// <returns>
        /// Return LibraryBookDetails
        /// </returns>
        public static BookDetails GetBookDetails(string isbnID)
        {
            const string BOOK_IS_NOT_AVAILABLE_MESSAGE = "Book is not available";
            const string NULL_STRING = "null";

            using (AWSECommerceServicePortTypeClient amazonClient = new AWSECommerceServicePortTypeClient())
            {
                var request = new ItemSearchRequest();
                request.SearchIndex = Constants.SEARCH_INDEX_BOOKS;
                request.Keywords = isbnID;
                request.ResponseGroup = new string[] { Constants.RESPONSE_GROUP_LARGE };

                var itemSearch = new ItemSearch();
                itemSearch.Request = new ItemSearchRequest[] { request };
                itemSearch.AssociateTag = string.Empty;
                itemSearch.AWSAccessKeyId = ConfigurationManager.AppSettings[APP_CONFIG_AMAZON_ACCESS_KEY_SECTION];

                ItemSearchResponse response = amazonClient.ItemSearch(itemSearch);

                if (response.Items[0].Item == null)
                {
                    throw new ArgumentNullException(BOOK_IS_NOT_AVAILABLE_MESSAGE);
                }

                var item = response.Items.FirstOrDefault().Item.FirstOrDefault();
                var bookDetails = new BookDetails()
                {
                    Title=item.ItemAttributes.Title,
                    ImagePath = item.LargeImage.URL,
                    Authors = item.ItemAttributes.Author.ToList(),
                    EditorialReview =item.EditorialReviews.FirstOrDefault().Content,
                    DetailsPath = item.DetailPageURL.Replace(NULL_STRING,string.Empty),
                    ISBN=item.ItemAttributes.ISBN,
                    EAN=item.ItemAttributes.EAN,
                    Publisher=item.ItemAttributes.Publisher,
                    ReleaseDate=DateTime.Parse(item.ItemAttributes.PublicationDate),
                    NumberOfPages=item.ItemAttributes.NumberOfPages,
                    Edition=item.ItemAttributes.Edition
                };

                return bookDetails;
            }
        }
        private static string GetShortDescription(string fullDescription)
        {
            int endDescPosition = fullDescription.IndexOf(BEGIN_HEADER_TAG_FRAGMENT);
            return fullDescription.Substring(0, endDescPosition);
        }

    }
}